
layui.use(['layer', 'form', 'table', 'util', 'admin', 'laypage', 'element'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var util = layui.util;
    var admin = layui.admin;
    var laypage = layui.laypage;
    var element = layui.element;
    var Authorization;//请求接口用
    var count = 0;//总个数
    var current = 1;//当前页
    var fistRenderPage = true;//是否第一次渲染分页
    var username = "";//搜索框值
    var regionCatalogType;
    var role = [];//权限转换
    var region={//区划保存（新增和编辑同时使用）
        regionLevel:"",//区划等级
        regionSelected:[]//选中区划
    }
    //获取权限方法
    var userInfo = getAuthority()
    if (userInfo) {
        var access_token = localStorage.getExpire("access_token");
        var token_type = localStorage.getExpire("token_type");
        Authorization = token_type + " " + access_token;
        getUserList();
        getRegionVersion();
    }
    var regionSetting = {//树设置
        identValueSetting: {//区划树设置
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false,
                drag: {
                    isCopy: true,
                    isMove: false,
                    prev: false,
                    next: false,
                    inner: false
                }
            },
            check: {
                enable: true, //是否显示radio/checkbox
                autoCheckTrigger: true,//关联勾选时是否触发 beforeCheck / onCheck 事件回调函数
                chkStyle: "checkbox",//值为checkbox或者radio表示
                radioType: "all",
                chkboxType: { "Y": "", "N": "" }//表示父子节点的联动效果，不联动
            },
            data: {
                key: {
                    children: "sub",
                    isParent: "isParent",
                    title: "name"
                },
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onExpand: onExpandIdentValueTree,
                onCheck: identValueOnCheck,
                beforeCheck:identValueBeforeCheck
            },
            view: {
                showIcon: false
            }
        },
    }
    // 添加
    $('#btnAddUser').click(function () {
        showAddUserModel();
    });

    //刷新
    $('#btnRefreshUser').click(function () {
        getUserList();
    });

    // 搜索
    form.on('submit(formSubSearchUser)', function (data) {
        username = data.field.username;
        current = 1;
        fistRenderPage = true;
        getUserList();
    });

    // 工具条点击事件
    table.on('tool(tableUser)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') { // 修改
            showEditModel(data);
        } else if (layEvent === 'del') { // 删除
            doDel(data);
        }
    });
    //转换菜单集合数据
    function regionZhuanHuan(tdata, resData) {
        if (Array.isArray(tdata) && tdata.length > 0) {
            tdata.forEach(function (v, i) {
                var newValue = v.name;
                resData[i] = v;
                resData[i].title = newValue;
                resData[i].icon = "";
                resData[i].isParent = "true";
                resData[i].sub = [];
            });
        }
    }
    //获取区划值
    function getTreeTable(obj, callback) {
        if(!obj.rcid && !obj.value){
            return false
        }
        var req = {
            rcId: obj.rcid ? obj.rcid : obj.value,
            regionCode: obj.code || "",
        };
        axios.get(regionUrl.info.queryRegion, {
            params: req,
            headers: {
                'Authorization': Authorization
            }
        })
            .then(function (response) {
                if (response.status == 200 && response.data.message == "success") {
                    var data = response.data.data;
                    var resArr = [];
                    regionZhuanHuan(data, resArr);
                    parCode = '';
                    par_id = '';
                    menuDetails = {};
                    // IdentValueTree = resArr;
                    callback(resArr)
                }
            })
            .catch(function (error) {
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            });
    }
    //查询下方区划树的下级区划值（懒加载）
    function onExpandIdentValueTree(event, treeId, treeNode) {

        if (treeNode.sub.length > 0 || treeNode.hasChild == '0') {
            return false
        }
        getTreeTable(treeNode, function (resArr) {
            const treeObj = $.fn.zTree.getZTreeObj("region_tree");
            treeNode = treeObj.addNodes(treeNode, resArr);
        });
    }
    function identValueBeforeCheck(treeId, treeNode) {
        let treeLevel = getRegionLevel(treeNode.code);
        if(region.regionLevel!=""&&region.regionLevel!=treeLevel){
            layer.msg("请选择同一层级区划或先清空上次选中", { icon: 2 });
            return false;
        }else{
            return true;
        }
    }
    //选中分组值
    function identValueOnCheck(event, treeId, treeNode) {
        menuDetails = treeNode
        parCode = treeNode.code
        par_id = treeNode.id
        IdentValueSelected = treeNode;
        let treeLevel = getRegionLevel(treeNode.code);
        if(treeNode.checked){//选中
            region.regionSelected.push(treeNode)
            region.regionLevel = treeLevel;
        }else{//取消
            for(let i in region.regionSelected){
                let regionObj = (region.regionSelected)[i];
                if(regionObj.code == treeNode.code){
                    region.regionSelected.splice(i,1)
                    if(region.regionSelected.length==0){
                        region.regionLevel="";
                    }
                    return;
                }
            }
        }
    }
    //转换菜单集合数据
    function zhuanHuanTree(tdata, sysResourceIds, resData) {
        if (Array.isArray(tdata) && tdata.length > 0) {
            tdata.forEach(function (v, i) {
                var newValue = v.name;
                resData[i] = v;
                resData[i].title = newValue;
                resData[i].icon = "";
                if (sysResourceIds.includes(v.id)) {
                    resData[i].checked = true;
                } else {
                    resData[i].checked = false;
                }
                var arr = [];
                zhuanHuanTree(v.children, sysResourceIds, arr);
                resData[i].children = arr;
            });
        }
    }
    //转换选中的菜单树数据，获取ID
    function initTreeCheckData(tdata, resData) {
        if (Array.isArray(tdata) && tdata.length > 0) {
            tdata.forEach(function (v, i) {
                resData.push(v.id);
                // initTreeCheckData(v.children,resData);
            });
        }
    }
    // 显示编辑弹窗
    function showEditModel(mUser) {

        getUserRole(function (roleData) {
            admin.open({
                type: 1,
                area: ['400px', '700px'],
                title: (mUser ? '编辑' : '新增') + '用户',
                content: $('#modelUser').html(),
                success: function (layero, dIndex) {
                    if(mUser.sysUserRegionsVos.length){
                        mUser.regionVersion = mUser.sysUserRegionsVos[0].rcid;
                        mUser.regionRcode = mUser.sysUserRegionsVos[0].region_code;
                        getTreeTable(mUser.sysUserRegionsVos[0], function (resArr) {
                            $.fn.zTree.init($("#region_tree"), regionSetting.identValueSetting, resArr);
                        });
                    }
                    //回显版本
                    //区划版本
                    let new_str = template("template-version-tmpl", { regionCatalogType });
                    document.getElementById("regionVersion").innerHTML = new_str;
                    form.render('select');
                    //初始角色
                    var str = template("role-tpl", roleData);
                    document.getElementById("roleName-box").innerHTML = str;

                    if (mUser) {//是编辑
                        $(layero).children('.layui-layer-content').css('overflow', 'visible');

                        for (var i = 0; i < mUser.sysRoleVoList.length; i++) {
                            role[i] = mUser.sysRoleVoList[i].roleId;
                            var roleobj = "role" + mUser.sysRoleVoList[i].roleId;
                            mUser[roleobj] = true;
                        }
                        var roleArr = [];
                        for (var i in roleData.data) {
                            var obj = "role" + roleData.data[i].roleId;
                            roleArr.push(obj);
                        }
                        // 回显数据
                        form.val('modelUserForm', mUser);
                        form.render('select');
                        //编辑树
                        var ztreeSetting = {
                            check: {
                                enable: true, //是否显示radio/checkbox
                                autoCheckTrigger: false,
                                chkStyle: "radio",//值为checkbox或者radio表示
                                radioType: "all",
                                chkboxType: { "Y": "", "N": "" }//表示父子节点的联动效果，不联动
                            },
                            data: {
                                simpleData: {
                                    enable: true//使用简单数据模式(Array)
                                }
                            },
                            view: {
                                showIcon: false
                            }
                        };
                        getOrganization([], function (treeData) {
                            //菜单树渲染
                            $.fn.zTree.init($("#roleMenuTree"), ztreeSetting, treeData);
                            //设置默认选中子节点
                            var zTree = $.fn.zTree.getZTreeObj("roleMenuTree");
                            if(mUser.sysOrganizationVos.length){
                                var node = zTree.getNodeByParam("id",mUser.sysOrganizationVos[0].id);
                                zTree.checkNode(node, true, true);
                            }

                            let rcid = "";
                            let rcname = "";
                            form.on('select(regionVersion)', function (select_data) {
                                //获取版本信息
                                rcid = select_data.value;
                                if(rcid==""){//没有版本清空区划
                                    rcname = "";
                                    $.fn.zTree.init($("#region_tree"), regionSetting.identValueSetting, []);
                                }else{
                                    regionCatalogType.map(type=>{
                                        if(type.id == rcid){
                                            rcname = type.name;
                                        }
                                    })
                                    //清空选中的级别
                                    region.regionLevel = "";
                                    region.regionSelected = [];
                                    //获取区划树
                                    getTreeTable(select_data, function (resArr) {
                                        $.fn.zTree.init($("#region_tree"), regionSetting.identValueSetting, resArr);
                                    });
                                }
                            })
                            // 新增用户
                            form.on('submit(modelSubmitUser)', function (data) {
                                var treeObj = $.fn.zTree.getZTreeObj("roleMenuTree");
                                var treeRegion = $.fn.zTree.getZTreeObj("region_tree");

                                var checkedNodes = ""
                                if(treeObj){
                                    checkedNodes = treeObj.getCheckedNodes(true);
                                }
                                var sysResourceIds = [];
                                initTreeCheckData(checkedNodes, sysResourceIds);
                                var checkRegion = ""
                                if(treeRegion){
                                    checkRegion = treeRegion.getCheckedNodes(true);
                                }
                                var checkRegionList = [];
                                initTreeCheckData(checkRegion, checkRegionList);
                                layer.load(2);
                                //获得菜单选中的节点
                                var req = JSON.stringify(mUser);
                                var param = JSON.parse(req);
                                param.username = data.field.username;
                                param.delFlag = data.field.delFlag;
                                param.mobile = data.field.mobile;
                                var role = [];
                                for (var i = 0; i < roleData.data.length; i++) {
                                    if (data.field.hasOwnProperty(roleArr[i])) {
                                        role.push({
                                            roleId: data.field[roleArr[i]]
                                        })
                                    }
                                }
                                param.role = role;//传给后台字段要求
                                param.sysRoleVoList = role;//传给后台字段要求

                                axios({
                                    method: 'put',
                                    url: systemUrl.editUser,
                                    data: param,
                                    headers: {
                                        'Authorization': Authorization
                                    }
                                })
                                    .then(function (res) {
                                        if (res.status == 200 && res.data.message == "success") {
                                            if(checkRegion.length){
                                                let region_codes = [];
                                                checkRegion.map(region=>{
                                                    region_codes.push(region.code)
                                                })
                                                let userRegion = {
                                                    rcid:rcid,
                                                    rcname:rcname,
                                                    username:param.username,
                                                    region_codes:region_codes.toString()
                                                }
                                                addUserRegion(userRegion,function (d) {
                                                    if(d){
                                                        layer.close(dIndex);
                                                        layer.closeAll('loading');
                                                    }else {
                                                        layer.close(dIndex);
                                                        layer.closeAll('loading');
                                                        layer.msg("操作成功", { icon: 1 });
                                                        getUserList();
                                                    }
                                                })
                                            }else {
                                                layer.close(dIndex);
                                                layer.closeAll('loading');
                                                layer.msg("操作成功", { icon: 1 });
                                                getUserList();
                                            }
                                            if(checkedNodes.length){
                                                let userOrganzation = {
                                                    user_name:param.username,
                                                    organizatonIds:checkedNodes[0].id
                                                }
                                                addOrganization(userOrganzation,function (b) {
                                                    if(b){
                                                        layer.close(dIndex);
                                                        layer.closeAll('loading');
                                                    }else {
                                                        layer.close(dIndex);
                                                        layer.closeAll('loading');
                                                        layer.msg("操作成功", { icon: 1 });
                                                        getUserList();
                                                    }

                                                })
                                            }else {
                                                layer.close(dIndex);
                                                layer.closeAll('loading');
                                                layer.msg("操作成功", { icon: 1 });
                                                getUserList();
                                            }
                                        } else {
                                            layer.closeAll('loading');
                                            layer.msg(res.data.message, { icon: 2 });
                                        }
                                    })
                                    .catch(function (error) {
                                        console.log(error);
                                        if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                                            // 进行重新登陆
                                            localStorage.clear();
                                            top.location.href=loginPageUrl;
                                        }
                                    })
                                return false;
                            });
                        })
                        // 表单提交事件
                        form.on('submit(modelSubmitUser)', function (data) {

                        });
                    }
                }
            });
        });
    }
    // 显示新增弹窗
    function showAddUserModel() {
        getUserRole(function (roleData) {
            admin.open({
                type: 1,
                title: '新增用户',
                area: ['400px', '700px'],
                content: $('#addUserModel').html(),
                success: function (layero, dIndex) {
                    var ztreeSetting = {
                        check: {
                            enable: true, //是否显示radio/checkbox
                            autoCheckTrigger: false,
                            chkStyle: "radio",//值为checkbox或者radio表示
                            radioType: "all",
                            chkboxType: { "Y": "", "N": "" }//表示父子节点的联动效果，不联动
                        },
                        data: {
                            simpleData: {
                                enable: true//使用简单数据模式(Array)
                            }
                        },
                        view: {
                            showIcon: false
                        }
                    };
                    getOrganization([], function (treeData) {
                        //菜单树渲染
                        $.fn.zTree.init($("#roleMenuTree"), ztreeSetting, treeData);
                        //区划版本
                        let new_str = template("template-version-tmpl", { regionCatalogType });
                        document.getElementById("regionVersion").innerHTML = new_str;
                        form.render('select');
                        let rcid = "";
                        form.on('select(regionVersion)', function (select_data) {
                            rcid = select_data.value
                            getTreeTable(select_data, function (resArr) {
                                $.fn.zTree.init($("#region_tree"), regionSetting.identValueSetting, resArr);
                            });
                        })
                        // 新增用户
                        form.on('submit(modelSubmitUser)', function (data) {
                            var treeObj = $.fn.zTree.getZTreeObj("roleMenuTree");
                            var treeRegion = $.fn.zTree.getZTreeObj("region_tree");

                            var checkedNodes = ""
                            if(treeRegion){
                                checkedNodes = treeObj.getCheckedNodes(true);
                            }
                            var sysResourceIds = [];
                            initTreeCheckData(checkedNodes, sysResourceIds);
                            var checkRegion = ""
                            if(treeRegion){
                                checkRegion = treeRegion.getCheckedNodes(true);
                            }

                            var checkRegionList = [];
                            initTreeCheckData(checkRegion, checkRegionList);
                            //获得菜单选中的节点

                            layer.load(2);
                            var param = {};
                            param.username = data.field.username;
                            param.password = data.field.password;
                            param.mobile = data.field.mobile;

                            var role = [];
                            for (var i = 0; i < roleData.data.length; i++) {
                                if (data.field.hasOwnProperty(roleArr[i])) {
                                    role.push({
                                        roleId: data.field[roleArr[i]]
                                    })
                                }
                            }
                            param.role = role;//传给后台字段要求
                            param.sysRoleVoList = role;//传给后台字段要求
                            axios({
                                method: 'POST',
                                url: systemUrl.editUser,
                                data: param,
                                headers: {
                                    'Authorization': Authorization
                                }
                            })
                                .then(function (res) {
                                    if (res.status == 200 && res.data.message == "success") {
                                        if(checkRegion.length){
                                            let userRegion = {
                                                rcid:rcid,
                                                username:param.username,
                                                region_codes:checkRegion[0].code
                                            }
                                            addUserRegion(userRegion,function (d) {
                                                if(d){
                                                    layer.close(dIndex);
                                                    layer.closeAll('loading');
                                                }else { //添加区划
                                                    layer.close(dIndex);
                                                    layer.closeAll('loading');
                                                    layer.msg("新增成功", { icon: 1 });
                                                    getUserList();
                                                }

                                            })
                                        }else {
                                            layer.close(dIndex);
                                            layer.closeAll('loading');
                                            layer.msg("新增成功", { icon: 1 });
                                        }
                                        if(checkedNodes.length){
                                            let userOrganzation = {
                                                user_name:param.username,
                                                organizatonIds:checkedNodes[0].id
                                            }
                                            addOrganization(userOrganzation,function (b) {
                                                if(b){
                                                    layer.close(dIndex);
                                                    layer.closeAll('loading');
                                                }else {
                                                    layer.close(dIndex);
                                                    layer.closeAll('loading');
                                                    layer.msg("新增成功", { icon: 1 });
                                                    getUserList();
                                                }

                                            })
                                        }else {//添加用户权限和部门组织
                                            layer.close(dIndex);
                                            layer.closeAll('loading');
                                            layer.msg("新增成功", { icon: 1 });
                                        }
                                    } else {
                                        layer.close(dIndex);
                                        layer.closeAll('loading');
                                        layer.msg(res.data.message, { icon: 2 });
                                    }
                                })
                                .catch(function (error) {
                                    console.log(error);
                                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                                        // 进行重新登陆
                                        localStorage.clear();
                                        top.location.href=loginPageUrl;
                                    }
                                })
                            return false
                        });
                    })

                    //初始角色
                    var str = template("adduser-role-tpl", roleData);
                    document.getElementById("add-RoleName-box").innerHTML = str;

                    $(layero).children('.layui-layer-content').css('overflow', 'visible');

                    var roleArr = [];
                    for (var i in roleData.data) {
                        var obj = "role" + roleData.data[i].roleId;
                        roleArr.push(obj);
                    }
                    form.render("checkbox");
                }
            });
        });
    }
    // 删除
    function doDel(data) {
        layer.confirm('确定要删除用户“' + data.username + '”吗？', {
            skin: 'layui-layer-admin',
            shade: .1
        }, function (i) {
            layer.close(i);
            layer.load(2);
            axios({
                method: 'delete',
                url: systemUrl.deleteUser + data.userId,
                headers: {
                    'Authorization': Authorization
                }
            })
                .then(function (res) {
                    if (res.status == 200 && res.data.message == "success") {
                        layer.msg("成功删除", { icon: 1 });
                        getUserList();
                    } else {
                        layer.msg(res.data.message, { icon: 2 });
                    }
                    layer.closeAll('loading');
                })
                .catch(function (error) {
                    console.log(error);
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                })
        });
    }

    //获取用户列表
    function getUserList() {
        var req = {
            current: current,
            size: pageSize,
            username: username,
            isAsc: false
        };
        axios.get(systemUrl.userList, {
            params: req,
            headers: {
                'Authorization': Authorization
            }
        })
            .then(function (response) {
                if (response.status == 200 && response.data.message == "success") {
                    var data = response.data.data;
                    renderTable(data);
                }
            })
            .catch(function (error) {
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            });
    }
    //渲染表格
    function renderTable(data) {
        table.render({
            elem: '#tableUser',
            cellMinWidth: 100,
            data: data.records,
            cols: [[
                { type: 'numbers', title: '#' },
                { field: 'userId', title: 'id' },
                { field: 'username', title: '用户名' },
                { field: 'mobile', title: '手机号', minWidth: 300 },
                {
                    field: 'sysRoleVoList', templet: function (d) {
                        if(d.sysRoleVoList.length){
                            return d.sysRoleVoList[0].roleName;
                        }else {
                            return ""
                        }

                    }, title: '角色'
                },
                {
                    field: 'delFlag', templet: function (d) {
                        var status = d.delFlag;
                        const statusMap = {
                            0: '有效',
                            1: '无效',
                            2: '初始化',
                            9: '锁定'
                        }
                        return statusMap[status]
                    }, title: '状态'
                },
                { align: 'center', toolbar: '#tableBarUser', title: '操作', }
            ]]
        });

        //第一次才渲染分页
        if (fistRenderPage) {
            laypage.render({
                elem: 'page-box'
                , count: data.total,
                limit: pageSize,
                groups: 1,
                current: current
                , layout: ['count', 'prev', 'page', 'next']
                , jump: function (obj, first) {
                    if (!first) {
                        current = obj.curr;
                        fistRenderPage = false;
                        getUserList();
                    }
                }
            });
        }
    }
    //获取用户角色
    function getUserRole(callback) {
        axios.get(systemUrl.userRole, {
            headers: {
                'Authorization': Authorization
            }
        })
            .then(function (response) {
                if (response.status == 200 && response.data.message == "success") {
                    var data = response.data;
                    callback(data)
                }
            })
            .catch(function (error) {
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            });
    }
    //获取所有组织列表
    function getOrganization(sysResourceIds, callback) {
        let req = {
            region_code: "510000000000"
        }
        axios.get(systemUrl.organization.organizationTree, {
            params: req,
            headers: {
                'Authorization': Authorization
            },
        })
            .then(function (response) {
                if (response.status == 200 && response.data.message == "success") {
                    var data = response.data.data;
                    var resArr = [];
                    zhuanHuanTree(data, sysResourceIds, resArr);
                    callback(resArr);
                }
            })
            .catch(function (error) {
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            });
    }
    //获取区划版本
    function getRegionVersion() {
        var req = {
            current: 1,
            size: 100,
            flag: 0
        };
        axios.get(regionUrl.catalog.catalogList, {
            params: req,
            headers: {
                'Authorization': Authorization
            }
        })
            .then(function (response) {
                if (response.status == 200 && response.data.message == "success") {
                    regionCatalogType = response.data.data.records;
                }
            })
            .catch(function (error) {
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            });

    }
    //添加区划
    function addUserRegion(userRegion,callback) {
        axios({
            method: 'PUT',
            url: systemUrl.addUserRrgion,
            data: $.param(userRegion),
            headers: {
                'Authorization': Authorization
            }
        })
            .then(function (res) {
                if (res.status == 200 && res.data.message == "success") {
                    callback()
                }else{
                    layer.msg(res.data.message, { icon: 2 });
                }
            })
            .catch(function (error) {
                callback('error')
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            })
    }
    //用户部门关系添加
    function addOrganization(userOrganzation,callback) {
        axios({
            method: 'PUT',
            url: systemUrl.addOrganization,
            data: $.param(userOrganzation),
            headers: {
                'Authorization': Authorization
            }
        })
            .then(function (res) {
                if (res.status == 200 && res.data.message == "success") {
                    callback()
                }else{
                    callback('err')
                    layer.msg(res.data.message, { icon: 2 });
                }
            })
            .catch(function (error) {
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            })
    }

    /**
     * 获取区划代码对应的区划等级
     * @param {String} regionCode 区划代码
     */
    function getRegionLevel(regionCode) {
        let regionlevel = -1;
        // 此处判断需要12位完整区划
        if (regionCode.length === 12) {
            if (regionCode === "000000000000") {
                regionlevel = 1;
                //全国
            } else if (regionCode.toString().lastIndexOf("0000000000") !== -1) {
                regionlevel = 2;
                // 省
            } else if (regionCode.toString().lastIndexOf("00000000") !== -1) {
                regionlevel = 3;
                // 市
            } else if (regionCode.toString().lastIndexOf("000000") !== -1) {
                regionlevel = 4;
                // 区县
            } else if (regionCode.toString().substring(9, 12) === "000") {
                regionlevel = 5;
                // 镇
            } else {
                //村（居委会）
                regionlevel = 6;
            }
        }else if(regionCode.length === 15){//普查小区
            regionlevel = 7;
        }else if(regionCode.length === 18){ // 12位区划代码 + 3位普查小区代码 + 3为建筑物编码
            regionlevel = 8;
        }
        // 如果行政区划需要特殊判断的，后续在这里加入
        return regionlevel;
    }
});
